<?php 
class ControllerProductSanPham extends Controller {  
	public function index() { 
		$this->language->load('product/manufacturer');
		$this->load->model('catalog/product');
		$this->load->model('tool/seo_url'); 
		$this->load->model('tool/image');
		
		$this->document->breadcrumbs = array();
		
      	$this->document->breadcrumbs[] = array(
        	'href'      => '',
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);

      		$this->document->breadcrumbs[] = array(
        		'href'      => $this->model_tool_seo_url->rewrite($this->model_tool_seo_url->http('product/sanpham&sanpham=1')),
        		'text'      => 'Sản phẩm',
        		'separator' => $this->language->get('text_separator')
      		);
					  		
			$this->document->setTitle ( "Sản phẩm" );
									
			$this->data['heading_title'] = 'Sản phẩm';

			$this->data['text_sort'] = $this->language->get('text_sort');
				if (isset($this->request->get['page'])) {
					$page = $this->request->get['page'];
				} else {
					$page = 1;
				}				

				if (isset($this->request->get['sort'])) {
					$sort = $this->request->get['sort'];
				} else {
					$sort = 'pd.name';
				}

				if (isset($this->request->get['order'])) {
					$order = $this->request->get['order'];
				} else {
					$order = 'ASC';
				}
			
			$product_total = $this->model_catalog_product->getProductSP($sort, $order, ($page - 1) * 24, 24);
			
			if ($product_total) {

				$this->load->model('catalog/review');
				
				$this->data['products'] = array();
        		
				$results = $this->model_catalog_product->getProductSP($sort, $order, ($page - 1) * 24, 24);
				
        		
        //------- get category show to home page -----------------------
        $category = $this->model_catalog_product->getCategoryAndProduct();

        if (count($category) > 0) {
            foreach ($category as $key => $value) {
                $category[$key]['href'] = $this->model_tool_seo_url->rewrite($this->model_tool_seo_url->http('product/category&path=' . $value['category_id']));
                foreach ($value['products'] as $k => $v) {
                    $category[$key]['products'][$k]['href'] = $this->model_tool_seo_url->rewrite($this->model_tool_seo_url->http('product/product&path=' . $this->model_tool_seo_url->khongdau($category[$key]['name']) . '&product_id=' . $v['product_id']));
                    $category[$key]['products'][$k]['img'] = $this->model_tool_image->resize($v['image'], 150, 160);
                }
            }
            $this->data['category'] = $category;
        }
        
				
				if (!$this->config->get('config_customer_price')) {
					$this->data['display_price'] = TRUE;
				} elseif ($this->customer->isLogged()) {
					$this->data['display_price'] = TRUE;
				} else {
					$this->data['display_price'] = FALSE;
				}

				$url = '';
		
				if (isset($this->request->get['page'])) {
					$url .= '&page=' . $this->request->get['page'];
				}	
				
				$this->data['sorts'] = array();
				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_name_asc'),
					'value' => 'pd.name-ASC',
					'href'  => $this->model_tool_seo_url->rewrite($this->model_tool_seo_url->http('product/sanpham&sanpham=1&sort=pd.name&order=ASC'))
				);  
 
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_name_desc'),
					'value' => 'pd.name-DESC',
					'href'  => $this->model_tool_seo_url->rewrite($this->model_tool_seo_url->http('product/sanpham&sanpham=1&sort=pd.name&order=DESC'))
				);  

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_price_asc'),
					'value' => 'p.price-ASC',
					'href'  => $this->model_tool_seo_url->rewrite($this->model_tool_seo_url->http('product/sanpham&sanpham=1&sort=p.price&order=ASC'))
				); 

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_price_desc'),
					'value' => 'p.price-DESC',
					'href'  => $this->model_tool_seo_url->rewrite($this->model_tool_seo_url->http('product/sanpham&sanpham=1&sort=p.price&order=DESC'))
				); 


				$url = '';
		
				if (isset($this->request->get['sort'])) {
					$url .= '&sort=' . $this->request->get['sort'];
				}	

				if (isset($this->request->get['order'])) {
					$url .= '&order=' . $this->request->get['order'];
				}
				
				$pagination = new Pagination();
				$pagination->total = sizeof($product_total);
				$pagination->page = $page;
				$pagination->limit = 24; 
				$pagination->text = $this->language->get('text_pagination');
				$pagination->url = $this->model_tool_seo_url->rewrite($this->model_tool_seo_url->http('product/sanpham' . $url . '&page=%s'));
			
				$this->data['pagination'] = $pagination->render();

				$this->data['sort'] = $sort;
				$this->data['order'] = $order;
				
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/sanpham.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/product/sanpham.tpl';
				} else {
					$this->template = 'default/template/product/sanpham.tpl';
				}	
				
				$this->children = array(
					'common/header',
					'common/footer',
					'common/column_left',
					'common/column_right'
				);		
				
				$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));										
      		} else {
        		$this->document->title = $manufacturer_info['name'];

        		$this->data['heading_title'] = 'Sản phẩm';

        		$this->data['text_error'] = $this->language->get('text_empty');

        		$this->data['button_continue'] = $this->language->get('button_continue');

        		$this->data['continue'] = $this->model_tool_seo_url->http('common/home');
		
				if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
					$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
				} else {
					$this->template = 'default/template/error/not_found.tpl';
				}
				
				$this->children = array(
					'common/header',
					'common/footer',
					'common/column_left',
					'common/column_right'
				);		
				
				$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));					
      		}
 
  	}
}
?>